package cn.lyjuan.android.mylibrary.domain;

import io.realm.Realm;
import io.realm.RealmModel;
import io.realm.RealmObject;
import io.realm.annotations.Ignore;
import io.realm.annotations.Index;
import io.realm.annotations.RealmClass;
import io.realm.annotations.Required;

/**
 * ID生成工具
 * Created by chad on 05/11/2017.
 */
@RealmClass
public class IdModel implements RealmModel
{
    @Required
    @Index
    private String schema;

    @Required
    private Integer id;

    @Ignore
    private Realm realm;

    /**
     * 初始ID
     */
    @Ignore
    public static final Integer DEF_INIT_ID = 0;

    public Integer increment()
    {
        if (!RealmObject.isManaged(this))
            throw new IllegalStateException("unrealm object");

        realm.beginTransaction();
        if (null == id || id < DEF_INIT_ID)
            id = DEF_INIT_ID;
        ++id;
        realm.commitTransaction();
        return id;
    }

    /**
     * 从realm中获取ID
     *
     * @param schema
     * @return
     */
    public static IdModel fromRealm(Realm realm, Class<? extends RealmModel> schema)
    {
        IdModel id = realm.where(IdModel.class).equalTo("schema", IdModel.schema(schema))
                .findFirst();

        if (null != id)
        {
            id.realm = realm;
            return id;
        }

        IdModel newId = IdModel.initForSchema(schema);

        realm.beginTransaction();
        newId = realm.copyToRealm(newId);// 普通对象转为 realm对象
        realm.commitTransaction();

        newId.realm = realm;
        return newId;
    }

    /**
     * 获取{@code schema}下一个ID
     * @param realm
     * @param schema
     * @return
     */
    public static Integer nextId(Realm realm, Class<? extends RealmModel> schema)
    {
        IdModel idModel = fromRealm(realm, schema);

        return idModel.increment();
    }

    /**
     * 获取表的schema字段值
     *
     * @param schema
     * @return
     */
    public static String schema(Class<? extends RealmModel> schema)
    {
        return schema.getSimpleName();
    }

    /**
     * 初始化一个ID
     *
     * @param schema
     * @return
     */
    private static IdModel initForSchema(Class<? extends RealmModel> schema)
    {
        IdModel id = new IdModel();
        id.setSchema(schema(schema));
        id.setId(DEF_INIT_ID);

        return id;
    }

    public IdModel()
    {
    }

    public IdModel(String schema, Integer id)
    {
        this.schema = schema;
        this.id = id;
    }

    public String getSchema()
    {
        return schema;
    }

    public void setSchema(String schema)
    {
        this.schema = schema;
    }

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }


    public Realm getRealm()
    {
        return realm;
    }

    public void setRealm(Realm realm)
    {
        this.realm = realm;
    }

    @Override
    public String toString()
    {
        return "IdModel{" +
                "schema='" + schema + '\'' +
                ", id=" + id +
                '}';
    }
}
